import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, ParamMap, Router } from '@angular/router';
import { switchMap } from 'rxjs/operators';
import { Message, ConfirmationService } from 'primeng/api';
import { ContentService } from '../../../layout/admin/service/content.service';
import { MessageUtilsService } from '../../../shared/services/message-utils.service';
import * as _ from 'lodash';
import { AttachmentUtilsService } from '../../../shared/services/attachment-utils.service';
import { ContentModuleKeys } from '../../../shared/config/app-settings';
@Component({
  selector: 'app-agent-guide-content',
  templateUrl: './agent-guide-content.component.html',
  styleUrls: ['./agent-guide-content.component.scss']
})
export class AgentGuideContentComponent implements OnInit {
  menuKey: string = '';
  msgs: Message[] = [];
  // tab1
  user_guide = { id: 0, content1: '', module: ContentModuleKeys.user_guide };
  training_module = { id: 0, content1: '', module: ContentModuleKeys.training_module };
  user_agreement = { id: 0, content1: '', module: ContentModuleKeys.user_agreement };
  // tab2
  agent_resources_instructions = { id: 0, content1: '', module: ContentModuleKeys.agent_resources_instructions };
  agent_resources_expression_of_insterest = { id: 0, content1: '', module: ContentModuleKeys.expression_of_interest };
  agent_resources_contract_of_sale = { id: 0, content1: '', module: ContentModuleKeys.contract_of_sale };
  agent_resources_deposit_form = { id: 0, content1: '', module: ContentModuleKeys.deposit_form };
  agent_resources_solicitor_contract = { id: 0, content1: '', module: ContentModuleKeys.solicitor_contract };
  agent_resources_management_agreement = { id: 0, content1: '', module: ContentModuleKeys.management_agreement };
  FAQs = {id: 0, content1:'', module: ContentModuleKeys.FQAs}
  useful_documents = { id: 0, content1: '', module: ContentModuleKeys.useful_documents}

  constructor(private contentService: ContentService,
    private messageUtilsService:MessageUtilsService,
    private attachmentUtilsService:AttachmentUtilsService,
    public route: ActivatedRoute, private router: Router) { }

  ngOnInit() {
    // this.route.params.subscribe
    // this.router.events.subscribe((data) =>{
    //   console.log(data);
    // });

    this.route.params.subscribe(params => {
      this.menuKey = params['menuKey']; // (+) converts string 'id' to a number
      console.log('menuKey : ' + this.menuKey);
      // In a real app: dispatch action to load the details here.
    });

    this.loadContent();
    console.log(this.useful_documents);


  }

  onSave() {
    const contentRichTextArray = [
      this.useful_documents
    ];
    this.updateContent(contentRichTextArray);
  }

  updateContent(contentRichTextArray) {
    this.contentService.updateContent(contentRichTextArray).subscribe(
      (res) => {
        console.log(res);
        this.parseResponseEntityArray(res);
        this.messageUtilsService.showInfo(this.msgs,"Update content text success!");
      },
      (err) => {
        console.log(err);
        this.messageUtilsService.showError(this.msgs,"Operation Failed.");
      }
    );
  }
  //server exchange
  loadContent() {
    this.contentService.getContent(ContentModuleKeys).subscribe(
      (res) => {
        this.parseResponseEntityArray(res);
        // this.user_guide = _.find(res, ['module', ContentModuleKeys.user_guide]);
      },
      (err) => {
        console.log(err);
      }
    );
  }


  fileUpload(belongToCategory, fileUploader, entity, deleteOrigin=false) {
    let attachment = {
      entity: entity,
      belongToID: entity.id,
      belongToCategory: belongToCategory,
      files: fileUploader.files
    };
    this.attachmentUtilsService.uploadFile(attachment, deleteOrigin).subscribe(
      (res) => {
        this.messageUtilsService.showInfo(this.msgs," Attahement update success");
        fileUploader.clear();
        entity['content_attachments'] = res.data;
      },
      (err) => {
        console.log(err);
        this.messageUtilsService.showError(this.msgs,"Operation Failed.");
      }
    );
  }

  deleteFile(attachment, entity?) {
    this.attachmentUtilsService.deleteAttachment(attachment).subscribe(
      (res) => {
        this.messageUtilsService.showInfo(this.msgs,attachment.originalFilename + " delete success");
        entity['content_attachments'] = res['data'];
        // this.initPropertyList();
      },
      (err) => {
        console.log(err);
        this.messageUtilsService.showError(this.msgs,"Operation Failed.");
      }
    );
  }

  parseResponseEntityArray(res) {
    let that = this;
    _.each(res, function (entity) {
      if (entity.module === ContentModuleKeys.user_guide) {
        that.user_guide = entity;
      } else if (entity.module === ContentModuleKeys.training_module) {
        that.training_module = entity;
      } else if (entity.module === ContentModuleKeys.user_agreement) {
        that.user_agreement = entity;
      } else if (entity.module === ContentModuleKeys.agent_resources_instructions) {
        that.agent_resources_instructions = entity;
      } else if (entity.module === ContentModuleKeys.expression_of_interest) {
        that.agent_resources_expression_of_insterest = entity;
      } else if (entity.module === ContentModuleKeys.contract_of_sale) {
        that.agent_resources_contract_of_sale = entity;
      } else if (entity.module === ContentModuleKeys.deposit_form) {
        that.agent_resources_deposit_form = entity;
      } else if (entity.module === ContentModuleKeys.solicitor_contract) {
        that.agent_resources_solicitor_contract = entity;
      } else if (entity.module === ContentModuleKeys.management_agreement) {
        that.agent_resources_management_agreement = entity;
      } else if (entity.module === ContentModuleKeys.useful_documents) {
        console.log(entity)
        that.useful_documents = entity;
      } else if (entity.module === ContentModuleKeys.FQAs) {
        that.FAQs = entity;
      }

    })
  }

}
